using System;
using System.Net;
using Microsoft.SPOT;
using System.Net.Sockets;
using System.Text;


namespace eHouse
{
    class ComManager
    {
        private Socket socket;
        private string sentence;
        private string[] command;
        private bool stop;

        public ComManager(Socket socket)
        {
            this.socket = socket;
            sentence = "";
            stop = false;
        }

        public void run()
        {
            while (!stop)
            {
                try
                {
                    if (socket.Poll(-1, SelectMode.SelectRead))
                    {
                        if (socket.Available == 0)
                            return;

                        Byte[] inBuffer = new Byte[256];
                        Int32 bytesRead = socket.Receive(inBuffer, socket.Available, SocketFlags.None);
                        sentence = new String(Encoding.UTF8.GetChars(inBuffer));
                        Debug.Print(sentence);
                        command = sentence.Split('/');

                        if (command[0] == ("HELLO_SERVER"))
                        {                            
                            byte[] responseBytes = Encoding.UTF8.GetBytes("HELLO " + socket.RemoteEndPoint.ToString());
                            socket.Send(responseBytes);

                        }
                        else if (command[0] == ("SET_TIME"))
                        {
                            try
                            {
                                Microsoft.SPOT.Hardware.Utility.SetLocalTime(new DateTime(int.Parse(command[1]),int.Parse(command[2]),int.Parse(command[3]),int.Parse(command[4]),int.Parse(command[5]),int.Parse(command[6])));
                                byte[] responseBytes = Encoding.UTF8.GetBytes("CORRECT");
                                socket.Send(responseBytes);
                            }
                            catch (Exception)
                            {
                                byte[] responseBytes = Encoding.UTF8.GetBytes("ERROR");
                                socket.Send(responseBytes);
                            }
                        }
                        else if (command[0] == ("SET_IP"))
                        {
                            try
                            {
                                Drivers.ip = command[1];
                                byte[] responseBytes = Encoding.UTF8.GetBytes("CORRECT");
                                socket.Send(responseBytes);
                            }
                            catch (Exception)
                            {
                                byte[] responseBytes = Encoding.UTF8.GetBytes("ERROR");
                                socket.Send(responseBytes);
                            }
                        }
                        else if (command[0] == ("GET_LIGHTS"))
                        {

                            string response = "LIGHTS/";
                            response += "Kitchen/";
                            response += Drivers.getInstance().led_kitchen.Read().ToString().ToUpper();
                            response += "/Living room/";
                            response += Drivers.getInstance().led_living.Read().ToString().ToUpper();
                            response += "/Bedroom/";
                            response += Drivers.getInstance().led_bedroom.Read().ToString().ToUpper();
                            response += "/Bathroom/";
                            response += Drivers.getInstance().led_bathroom.Read().ToString().ToUpper();

                            byte[] responseBytes = Encoding.UTF8.GetBytes(response);
                            socket.Send(responseBytes);
                        }
                        else if (command[0] == "SET_LIGHTS")
                        {

                            string room = command[1];
                            string state = command[2];
                            try
                            {
                                if (room == "KITCHEN")
                                {
                                    Drivers.getInstance().led_kitchen.Write(state == "TRUE");
                                }
                                else if (room == "LIVING ROOM")
                                {
                                    Drivers.getInstance().led_living.Write(state == "TRUE");
                                }
                                else if (room == "BEDROOM")
                                {
                                    Drivers.getInstance().led_bedroom.Write(state == "TRUE");
                                }
                                else if (room == "BATHROOM")
                                {
                                    Drivers.getInstance().led_bathroom.Write(state == "TRUE");
                                }
                                byte[] responseBytes = Encoding.UTF8.GetBytes("CORRECT");
                                socket.Send(responseBytes);
                            }
                            catch (Exception)
                            {
                                byte[] responseBytes = Encoding.UTF8.GetBytes("ERROR");
                                socket.Send(responseBytes);
                            }
                        }
                        else if (command[0] == "GET_TEMPERATURE")
                        {
                            string response = "TEMPERATURE/" + Drivers.getInstance().getTemperature();
                            response += "/Heating/" + Drivers.getInstance().led_heating.Read().ToString().ToUpper();
                            response += "/AC/" + Drivers.getInstance().led_AC.Read().ToString().ToUpper();

                            byte[] responseBytes = Encoding.UTF8.GetBytes(response);
                            socket.Send(responseBytes);
                        }
                        else if (command[0] == "SET_TEMPERATURE")
                        {
                            string sensor = command[1];
                            string state = command[2];

                            try
                            {
                                if (sensor == "HEATING")
                                {
                                    Drivers.getInstance().led_heating.Write(state == "TRUE");
                                }
                                else if (sensor == "AC")
                                {
                                    Drivers.getInstance().led_AC.Write(state == "TRUE");
                                }

                                byte[] responseBytes = Encoding.UTF8.GetBytes("CORRECT");
                                socket.Send(responseBytes);
                            }
                            catch (Exception)
                            {
                                byte[] responseBytes = Encoding.UTF8.GetBytes("ERROR");
                                socket.Send(responseBytes);
                            }
                        }
                        else if (command[0] == "SET_IRRIGATION")
                        {
                            string state = command[1];

                            try
                            {

                                Drivers.getInstance().led_irrigation.Write(state == "TRUE");

                                byte[] responseBytes = Encoding.UTF8.GetBytes("CORRECT");
                                socket.Send(responseBytes);
                            }
                            catch (Exception)
                            {
                                byte[] responseBytes = Encoding.UTF8.GetBytes("ERROR");
                                socket.Send(responseBytes);
                            }

                        }
                        else if (command[0] == "GET_WINDOWS")
                        {
                            string response = "WINDOWS/";
                            response += "Kitchen/";
                            response += (!Drivers.getInstance().window_kitchen.Read()).ToString().ToUpper();
                            response += "/Living room/";
                            response += (!Drivers.getInstance().window_living.Read()).ToString().ToUpper();
                            response += "/Bedroom/";
                            response += (!Drivers.getInstance().window_bedroom.Read()).ToString().ToUpper();
                            response += "/Bathroom/";
                            response += (!Drivers.getInstance().window_bathroom.Read()).ToString().ToUpper();

                            byte[] responseBytes = Encoding.UTF8.GetBytes(response);
                            socket.Send(responseBytes);
                        }
                        else if (command[0] == "GET_IRRIGATION")
                        {
                            string response = "IRRIGATION/";
                            response += Drivers.getInstance().led_irrigation.Read().ToString().ToUpper();
                            byte[] responseBytes = Encoding.UTF8.GetBytes(response);
                            socket.Send(responseBytes);
                        }
                        else if (command[0] == "QUIT")
                        {
                            byte[] responseBytes = Encoding.UTF8.GetBytes("QUIT");
                            socket.Send(responseBytes);
                            stop = true;
                        }
                        else
                        {
                            byte[] responseBytes = Encoding.UTF8.GetBytes("ERROR");
                            socket.Send(responseBytes);
                            Debug.Print("Command Error");
                        }
                    }

                }
                catch (Exception)
                {
                    stop = true;
                }
            }

            socket.Close();
        }
    }
}
